package org.thesis.dataAccess;

import javax.mail.Message;

import org.thesis.doituong.OneMail;
import org.thesis.xuly.ConvertMessageToList;
import org.thesis.xuly.cv_mess_hotmail_to_list;
import org.thesis.xuly.cv_mess_noibo_to_list;
import org.thesis.xuly.cv_mess_yahoo_to_list;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class db_email 
{
	
//	final static String DB_EMAIL_NAME = "db_emailqw";
	final static String table_name = "table_store_email";
	final static int VERSION = 1;
	
	final static String ID = "_id";
	final static String STT = "_stt";
	final static String FROM = "_from";
	final static String TO = "_to";
	final static String SUB = "_sub";
	final static String BODY = "_body";
	final static String TIME = "_time";
	final static String ATTACH = "_attach";
	final static String FAVORITE = "_favorite";
	final static String ENCRYPT = "_encrypt";
	final static String VOICE = "_voice";
	final static String READ = "_read";
	final static String STR_ATTACH = "_str_attach";
	
	final static String[]SQL_QUERY_ALL = new String[]
			{STT,FROM,TO,SUB,BODY,TIME,ATTACH,FAVORITE,ENCRYPT,VOICE,READ,STR_ATTACH};
	//
	Context context;
	
	String db_name;
	String sql;
	String tag = "db_email";
	SQLiteDatabase mydb;
	db_helper my_db_helper;
	String loai_email;
	public db_email(Context context, String db_name) {
		super();
		this.context = context;
		this.db_name = db_name;
		loai_email = db_name.substring(db_name.length()-5, db_name.length());
	}


	public class db_helper extends SQLiteOpenHelper
	{

		public db_helper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			
			// TODO Auto-generated method stub
			sql = "create table " + table_name + 
					"(_id integer PRIMARY KEY autoincrement, " +
					"_stt integer, "+
					"_from text, " +
					"_to text, "+
					"_sub text, "+
					"_body text, "+
					"_time text, "+
					"_attach numerric, "+
					"_favorite numeric, "+
					"_encrypt numeric, " +
					"_voice numeric," + 
					"_read numeric,"+
					"_str_attach text"+
					");";
			db.execSQL(sql);
			Log.v(tag, "Khoi tao table : " + table_name);
					
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			sql = "drop table if exists "+table_name;
			db.execSQL(sql);
			Log.v(tag, "Ungrade " + table_name);
			
		}
		
	} //end class db_helper
	
	public db_email open()
	{
		my_db_helper = new db_helper(context, db_name, null, VERSION);
		mydb = my_db_helper.getWritableDatabase();
		Log.v(tag, "Open");
		return this;
	}
	
	public void close()
	{
		my_db_helper.close();
		Log.v(tag, "Close");
	}
	
	public long insert(OneMail om)
	{
		long row_insert = 0;
		try
		{
			ContentValues cv = new ContentValues();
			cv.put(STT, om.getId());
			cv.put(FROM, om.getFrom());
			cv.put(TO, om.getTo());
			cv.put(SUB, om.getSub());
			cv.put(BODY, om.getBody());
//			Log.v(tag, "insert body : " + om.getBody());
			cv.put(TIME, om.getDateSend());
			Log.v(tag, "insert time ");
			cv.put(ATTACH, om.isFlagAttach());
			String ata = "";
			if(om.isFlagAttach())
			{
				ata = "true";
			}else 
				ata = "false";
			Log.v(tag, "insert att la:  " + ata);
			cv.put(FAVORITE,om.isFlagFavorite());
			Log.v(tag, "insert favorite ");
			if(om.isFlagEncrypt())
			{
				Log.v(tag, "insert Encrypt is true");
			}
			cv.put(ENCRYPT, om.isFlagEncrypt());
			Log.v(tag, "insert enc ");
			cv.put(VOICE, om.isFlagVoice());
			Log.v(tag, "insert voice ");
			cv.put(READ, om.isFlagRead());
			Log.v(tag, "insert read ");
			if(om.isFlagAttach()) // neu co att thi moi set , ko de null
			{
				cv.put(STR_ATTACH, om.getStr_attach());
//				Log.v(tag, "insert chuoi att : " + om.getStr_attach());
				Log.v(tag, "insert chuoi attach ...");
			}
			else
			{
//				cv.put(STR_ATTACH, "");
				Log.v(tag, "insert chuoi trong ..");
			}
			row_insert = mydb.insert(table_name, null, cv);
			Log.v(tag, "insert thanh cong : " + om.getSub());
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi insert " +e.toString());
		}
		return row_insert;
	}
	
	public Cursor query()
	{
		try
		{
			Log.v(tag, "Thuc hien query ..." );
			return mydb.query(table_name,SQL_QUERY_ALL, null, null, null, null, null);
			
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi get " +e.toString());
			return null;
		}
	}
	public Cursor query(int start, int end)
	{
		try
		{
			Log.v(tag, "Thuc hien query tu " + start + " den " + end );
			String selection = STT + " >= " + start + " AND " + STT + " <= " + end ;
			return mydb.query(table_name, SQL_QUERY_ALL, selection, null, null, null, null);
			
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi query start - end :  " +e.toString());
			return null;
		}
	}
	public boolean removeALl()
	{
		Log.i(tag, "remove CSDL my email...");
		return mydb.delete(table_name, null, null)>0;
	}
	public void save_msg_to_db(Message[] mess)
	{
		try
		{
			for(int i = mess.length -1 ; (i > mess.length - 11 && i >= 0 ) ; i --)
			{
//				if(i > 0)
				{
					if(loai_email.equals("gmail"))
					{
						ConvertMessageToList cv = new ConvertMessageToList();
						OneMail o_mail = cv.get1mail(mess, i);
						Log.v(tag, "insert gmail : " + i + " ===============================\n\r");
						insert(o_mail);
						
					}
					else if(loai_email.equals("yahoo"))
					{
						cv_mess_yahoo_to_list cv_yahoo = new cv_mess_yahoo_to_list();
						Log.v(tag, "insert yahoo mail : " + i + " ===============================\n\r");
						insert(cv_yahoo.get1mail(mess, i));
					}
					else if(loai_email.equals("tmail" ))
					{
						cv_mess_hotmail_to_list cv_hotmail = new cv_mess_hotmail_to_list();
						Log.v(tag, "insert hotmail mail :"+ i + " ===============================\n\r");
						insert(cv_hotmail.get1mail(mess, i));
					}
					else
					{
						cv_mess_noibo_to_list cv = new cv_mess_noibo_to_list();
						Log.v(tag, "insert mail Noi bo : "+ i + " ===============================\n\r");
						insert(cv.get1mail(mess, i));
					}
				}
			}
//			close();
		}
		catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "Loi save mail to db" + e.toString());
		}
	}
	
	//update favorite
	public int update_row_by_id(int id, boolean flag_favorite) 
	{
		try
		{
		ContentValues cv = new ContentValues();
		cv.put(FAVORITE,flag_favorite);
		Log.v(tag, "tien hanh update ");
		int n =  mydb.update(table_name, cv, "_stt = " + id, null);
		Log.v(tag, "update thanh cong..." +n);
		return n;
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "update that bai " + e.toString());
			return 0;
		}
	}
	
	// lay thong tin dua vao stt
	public Cursor query_get_by_stt(int _stt)
	{
		Log.i(tag, "get by stt...");
		String selection = STT + " = " + _stt;
		return mydb.query(true, table_name, SQL_QUERY_ALL, selection, 
				null, null, null, null, null);
	}
	
	public int remove_by_stt(int stt)
	{
		String whereClause = STT + " = " + stt;
		int i_row_delete = mydb.delete(table_name, whereClause, null);
		Log.v(tag, "delete thanh cong " +stt);
		return i_row_delete;
	}
}
